<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller
{
    public function index()
    {
        $title = '首页'; 
        $this->data($title);
        $this->display();
    }
    //栏目：php
    public function page_php()
    {
        $title = 'php'; 
        $this->data($title,'php');
        $this->display();
    }

    //栏目：linux
    public function page_linux()
    {
        $title = 'linux'; 
        $this->data($title,'linux');
        $this->display();
    }    
    //栏目:书单
    public function page_books()
    {
        $title = '书单'; 
        $this->data($title);
        $this->display();
    }
    //栏目：杂谈
    public function page_talks()
    {
        $title = '杂谈'; 
        $this->data($title,'杂谈');
        $this->display();
    }
    //栏目：留言
    public function page_msg()
    {
        $title = '留言'; 
        $this->data($title);
        $this->display();
    }
    //栏目：关于本站    
    public function page_about()
    {
        $title = '关于本站'; 
        $this->data($title);
        $this->display();
    }
    // 文章详情页显示
    public function article_detail()
    {
        $map['id']  = I('get.id');
        $article    = M('article');
        $article->where($map)->setInc('click_num');//每次访问页面点击量+1;
        $get_detail = $article->where($map)->select();
        $this->data();
        $tags=$this->get_tags($map['id']);
        $this->assign('get_detail',$get_detail);
        $this->assign('tag',$tags);
        $this->display();
    }

    private function data($title,$category)
    {        
        $recommend_article  = $this->get_recommend_article();
        $hot_article        = $this->get_hot_article();
        $links              = $this->get_links();
        $books              = $this->get_books();
        if($category!=null){
            $type=$category;
            $this->get_latest_article($type);
        }else{
            $this->get_latest_article();
        }
        $this->assign('title',$title);
        $this->assign('hot_article',$hot_article);
        $this->assign('recommend_article' ,$recommend_article);
        $this->assign('title',$title);
        $this->assign('hot_article',$hot_article);
        $this->assign('recommend_article' ,$recommend_article);
        $this->assign('links' ,$links);
        $this->assign('books' ,$books);
    }
    /**
     * 获取最新文章标题
     * @return array:以中级分类为键值的数组
     */
    private function get_latest_article($type)
    {
        $keyword = I('keyword','','trim');      //过滤开头结尾空格
        if($keyword){
            $map['title|content'] = ['like','%'.$keyword.'%'];
        }
        $map['status'] = 1;         // 1已审核，0未审核
        if($type!=null){$map['category'] = $type;}
        $article = M('article'); // 实例化article对象
        $count   = $article->where($map)->count();// 查询满足要求的总记录数
        $Page    = new \Think\Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show    = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list    = $article
        ->where($map)
        ->order('create_time desc')
        ->limit($Page->firstRow.','.$Page->listRows)
        ->select();
        $this->assign('keyword',$keyword);  //输出数据
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
    }
    /**
    * 获取推荐文章标题
    * @return array:以中级分类为键值的数组
    */
    private function get_recommend_article()
    {
        $map['recommend'] = 1;         // 是否推荐：1是，0否
        $latest_article   = M('article')
        ->where($map)
        ->order('create_time asc')
        ->limit(6)->select();       //获取最新文章
        return $latest_article;
    }
    /**
    * 获取推荐文章标题
    * @return array:以中级分类为键值的数组
    */
    private function get_hot_article()
    {
        $map['status'] = 1;         // 1已审核，0未审核
        $hot_article   = M('article')
        ->where($map)
        ->order('click_num desc')
        ->limit(6)->select();       //获取最新文章
        return $hot_article;
    }
    //获取友情链接
    private function get_links()
    {
        $map['status'] = 1;         // 1已审核，0未审核
        $links   = M('link')
        ->where($map)->select();       //获取最新文章
        return $links;
    }
    //获取书单
    private function get_books()
    {
        $map['status'] = 1;         // 1已审核，0未审核
        $books   = M('book')
        ->where($map)->select();       //获取最新文章
        return $books;
    }
    private function get_tags($aid)
    {
        $tag    = M('tag');
        $at     = M('article_tag');
        $rows   = $at->where(array('aid'=>$aid))
        ->getField('tid',true);
        $tids   = implode(",", $rows);
        if($tids!=''){
        $where['id'] = array('in',$tids);
        $tags   = $tag->where($where)->getField('name',true);
        return  $tags;
        }
    }    
}